package com.android.act;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.android.MyApp;
import com.android.R;
import com.android.common.Common;
import com.android.model.StationModel;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.baidu.mapapi.Projection;


public class ItemizedOverlayDemo extends MapActivity {
	
	static View mPopView = null;	// 点击mark时弹出的气泡View
	static TextView tv;
	static MapView mMapView = null;
	int iZoom = 0;
	OverItemT overitem = null;
	MyApp app;
	public static ArrayList<StationModel> shopList = null;
	
	public Drawable marker;
	public Drawable marker2;
	MyLocationOverlay mLocationOverlay = null; // 定位图层
	LocationListener mLocationListener = null;// onResume时注册此listener，onPause时需要Remove
	
	
	
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapviewdemo);
//        shopList = (ArrayList<StationModel>) getIntent().getSerializableExtra("shopList");
        initData();
        MyApp app = (MyApp)this.getApplication();
		if (app.mBMapMan == null) {
			app.mBMapMan = new BMapManager(getApplication());
			app.mBMapMan.init(app.mStrKey, new MyApp.MyGeneralListener());
		}
		app.mBMapMan.start();
        // 如果使用地图SDK，请初始化地图Activity
        super.initMapActivity(app.mBMapMan);
        
        mMapView = (MapView)findViewById(R.id.bmapView);
        mMapView.setBuiltInZoomControls(true);
        //设置在缩放动画过程中也显示overlay,默认为不绘制
        mMapView.setDrawOverlayWhenZooming(true);
        GeoPoint point =new GeoPoint((int)(39.90923*1e6), (int)(116.397428*1e6));
        mMapView.getController().setCenter(point);
  
        // 添加ItemizedOverlay
		  marker = getResources().getDrawable(R.drawable.pin_green);  //得到需要标在地图上的资源
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker .getIntrinsicHeight());   //为maker定义位置和边界
		 // 添加ItemizedOverlay
		  marker2 = getResources().getDrawable(R.drawable.pin_red);  //得到需要标在地图上的资源
		marker2.setBounds(0, 0, marker2.getIntrinsicWidth(), marker2.getIntrinsicHeight());   //为maker定义位置和边界
		
//		overitem = new OverItemT(marker,this, 3);
		overitem = new OverItemT(marker,marker2,this, shopList); 
		mMapView.getOverlays().add(overitem); //添加ItemizedOverlay实例到mMapView
		
		// 创建点击mark时的弹出泡泡
		mPopView=super.getLayoutInflater().inflate(R.layout.map_popview, null);
		  tv = (TextView)mPopView.findViewById(R.id.textView1);
		mMapView.addView(mPopView,new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, null, MapView.LayoutParams.TOP_LEFT));
		mPopView.setVisibility(View.GONE);
		iZoom = mMapView.getZoomLevel(); 
		// 添加定位图层
        mLocationOverlay = new MyLocationOverlay(this, mMapView);
		mMapView.getOverlays().add(mLocationOverlay);
//		
		GeoPoint pt = new GeoPoint((int)(Common.lat*1e6), (int)(Common.lon*1e6));
//		mMapView.getController().animateTo(pt);
		mMapView.getController().setCenter(pt);
        // 注册定位事件
//        mLocationListener = new LocationListener(){
//
//			@Override
//			public void onLocationChanged(Location location) {
//				if (location != null){
//					GeoPoint pt = new GeoPoint((int)(location.getLatitude()*1e6), (int)(location.getLongitude()*1e6));
//					mMapView.getController().animateTo(pt);
//				}
//			}
//        };
	}
	
	 
	private void initData() {
		shopList = new ArrayList<StationModel>(); 
		StationModel sm = new StationModel();
		 
		sm.setShopName("泉城广场站");
		sm.setVisited(true);
		sm.setShopLatitude(36.66123);
		sm.setShopLongitude(117.01616);
		shopList.add(sm);
		StationModel sm1 = new StationModel(); 
		sm1.setShopName("2号奥体中心站");
		sm1.setVisited(false);
		sm1.setShopLatitude(36.65616);
		sm1.setShopLongitude(117.114019);
		shopList.add(sm1);
		 
		StationModel sm2 = new StationModel();
		sm2.setShopName("3号站国际会展中心");
		sm2.setVisited(true);
		sm2.setShopLatitude(36.68484);
		sm2.setShopLongitude(117.11811);
		shopList.add(sm2);
		 
//		StationModel sm3 = new StationModel();
//		sm3.setShopName("4号站泉城公园");
//		sm3.setVisited(true);
//		sm3.setShopLatitude(36.67522);
//		sm3.setShopLongitude(117.02566);
//		shopList.add(sm3);
		 
		StationModel sm4 = new StationModel();
		sm4.setShopName("5号站皇亭体育馆");
		sm4.setVisited(true);
		sm4.setShopLatitude(36.66689);
		sm4.setShopLongitude(117.03147);
		shopList.add(sm4);
		
	}
	@Override
	protected void onPause() {
		MyApp app = (MyApp)this.getApplication();
		if(app.mBMapMan != null)
			app.mBMapMan.stop();
		super.onPause();
	}
	@Override
	protected void onResume() {
		MyApp app = (MyApp)this.getApplication();
//		app.mBMapMan.getLocationManager().requestLocationUpdates( mLocationListener);
		mLocationOverlay.enableMyLocation();
		mLocationOverlay.enableCompass(); // 打开指南针
		app.mBMapMan.start();
		super.onResume();
	}
	 

	@Override
	protected boolean isRouteDisplayed() { 
		 
		return false;
	}
}
class OverItemT extends ItemizedOverlay<OverlayItem> {

	public List<OverlayItem> mGeoList = new ArrayList<OverlayItem>();
	private Drawable marker;
	private Drawable marker2;
	private Context mContext;
	ArrayList<StationModel> shopList;

	private double mLat1 = 36.65931; // point1纬度
	private double mLon1 = 117.15763; // point1经度

	private double mLat2 = 36.64231;
	private double mLon2 = 117.15263;

	private double mLat3 = 36.63231;
	private double mLon3 = 117.15163;
	 

	public OverItemT(Drawable marker, Context context, int count) {
		super(boundCenterBottom(marker));

		this.marker = marker;
		this.mContext = context;
		ArrayList<StationModel> shopList;

		// 用给定的经纬度构造GeoPoint，单位是微度 (度 * 1E6)
		GeoPoint p1 = new GeoPoint((int) (mLat1 * 1E6), (int) (mLon1 * 1E6));
		GeoPoint p2 = new GeoPoint((int) (mLat2 * 1E6), (int) (mLon2 * 1E6));
		
		// 构造OverlayItem的三个参数依次为：item的位置，标题文本，文字片段
		mGeoList.add(new OverlayItem(p1, "P1", "point1"));
		mGeoList.add(new OverlayItem(p2, "P2", "point2"));
		if(count == 3)
		{
			GeoPoint p3 = new GeoPoint((int) (mLat3 * 1E6), (int) (mLon3 * 1E6));
			mGeoList.add(new OverlayItem(p3, "P3", "point3"));
		}
		populate();  //createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
	 
	}  

	public OverItemT(Drawable marker, Drawable marker2,Context context, ArrayList<StationModel> sl) {
		super(boundCenterBottom(marker));
		this. shopList = sl;
		this.marker2 = marker2;
		this.marker = marker;
		this.mContext = context; 
		 
		 for (int i = 0; i < shopList.size(); i++) {
			 StationModel sm = shopList.get(i); 
			 GeoPoint p3 = new GeoPoint((int) (sm.getShopLatitude() * 1E6), (int) (sm.getShopLongitude() * 1E6));
			 mGeoList.add(new OverlayItem(p3, sm.getShopName(), sm.getShopName()));
		}
		populate();  //createItem(int)方法构造item。一旦有了数据，在调用其它方法前，首先调用这个方法
	} 
	 

	public void updateOverlay()
	{
		populate();
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {

		// Projection接口用于屏幕像素坐标和经纬度坐标之间的变换
		Projection projection = mapView.getProjection(); 
		for (int index = size() - 1; index >= 0; index--) { // 遍历mGeoList
			OverlayItem overLayItem = getItem(index); // 得到给定索引的item

			String title = overLayItem.getTitle();
			// 把经纬度变换到相对于MapView左上角的屏幕像素坐标
			Point point = projection.toPixels(overLayItem.getPoint(), null); 

			// 可在此处添加您的绘制代码
			Paint paintText = new Paint();
//			paintText.setStyle(style)
			paintText.setStyle(Paint.Style.FILL);
			paintText.setColor(Color.BLUE);
		
			paintText.setTextSize(20);
			canvas.drawText(title, point.x-30, point.y, paintText); // 绘制文本
		}

		super.draw(canvas, mapView, shadow);
		//调整一个drawable边界，使得（0，0）是这个drawable底部最后一行中心的一个像素
//		boundCenterBottom(marker);
	}

	@Override
	protected OverlayItem createItem(int i) { 
		
		return mGeoList.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mGeoList.size();
	}
	@Override
	// 处理当点击事件
	protected boolean onTap(int i) {
		setFocus(mGeoList.get(i));
		// 更新气泡位置,并使之显示
		GeoPoint pt = mGeoList.get(i).getPoint();
		ItemizedOverlayDemo.mMapView.updateViewLayout( ItemizedOverlayDemo.mPopView, new MapView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,pt, MapView.LayoutParams.BOTTOM_CENTER));
		ItemizedOverlayDemo.mPopView.setVisibility(View.VISIBLE);
		final StationModel smm = shopList.get(i);
		ItemizedOverlayDemo.tv.setText(smm.getShopName());
		ItemizedOverlayDemo.tv.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent ii = new Intent(mContext, RoutePlan.class);
				 Bundle mBundle = new Bundle();
				 mBundle.putSerializable("StationModel", smm);
				 ii.putExtras(mBundle);
				mContext.startActivity(ii);
			}
		});
		
		Toast.makeText(this.mContext, mGeoList.get(i).getSnippet(),Toast.LENGTH_SHORT).show();
		return true;
	}

	@Override
	public boolean onTap(GeoPoint arg0, MapView arg1) { 
		// 消去弹出的气泡
		ItemizedOverlayDemo.mPopView.setVisibility(View.GONE);
		return super.onTap(arg0, arg1);
	}
	
	
	
}



